package com.dangdi.utils;

import com.dangdi.entity.PrintLine;
import com.dangdi.entity.PrintStyle;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.util.Map;

public class TicketPrinter implements Printable {
    private PrintStyle printStyle = new PrintStyle();

    public TicketPrinter(PrintStyle printStyle) {
        setPrintStyle(printStyle);
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        PrintLine pLine = new PrintLine();
        for (int i = 0; i < printStyle.getLine().size(); i++) {
            pLine = printStyle.getLine().get(i);
            g2d.setFont(new Font("Default", Font.PLAIN, pLine.getLineFontSize()));
            g2d.drawString(pLine.getLineStr(), pLine.getLineX(), pLine.getLineY());
            System.out.println(pLine.getLineStr());
        }
//        g2d.setFont(new Font("Default", Font.PLAIN, 14));
//        g2d.drawString("----------------С????-------------", 7, 20);
//        g2d.drawString("С????", 60, 40);
//        g2d.drawString("-------------------------------------", 7, 60);
//        g2d.drawString("???????" + "17602258294", 7, 80);
//        g2d.drawString("????????" + "11111", 7, 100);
//        g2d.drawString("-------------------------------------", 7, 120);
        return PAGE_EXISTS;
    }

    public void printTicket(PrintStyle pStyle) {
        Book book = new Book();
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);
        Paper p = new Paper();
        p.setImageableArea(0, 0, pStyle.getPageWidth(), pStyle.getPageHeight());
        pf.setPaper(p);
        book.append(new TicketPrinter(printStyle), pf);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);

        setPrintStyle(pStyle);
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println("??????");
            e.printStackTrace();
        }
    }

    public PrintStyle getPrintStyle() {
        return printStyle;
    }

    public void setPrintStyle(PrintStyle printStyle) {
        this.printStyle = printStyle;
    }

}
